

package com.mt.repair.controller.repairManagement;

import com.mt.common.core.web.JsonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.repair.dto.repairManagement.NoticeEditDto;
import com.mt.repair.entity.repairManagement.Notice;
import com.mt.repair.service.repairManagement.NoticeService;


@Api(tags = "通知公告")
@RestController
@RequestMapping("/api/repair/repairManagement/Notice")
@CrossOrigin(allowCredentials = "true")
public class NoticeController {
    private static Logger logger = LoggerFactory.getLogger(NoticeController.class);


    @Autowired
    private NoticeService noticeService;

    /**
     * 根据分页参数查询通知公告集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("分页查询通知公告")
    @ApiPageParam
    @PostMapping("/findNotices")
    public PageResultDTO findNotices(@RequestBody PageDTO pageDTO) {
        return this.noticeService.findNotices(pageDTO);
    }

    @ApiOperation("分页查询通知公告(不包含内容)")
    @ApiPageParam
    @PostMapping("/findNoticesNotContent")
    public PageResultDTO findNoticesNotContent(@RequestBody PageDTO pageDTO) {
        return this.noticeService.findNoticesNotContent(pageDTO);
    }

    /**
     * 根据ID查询指定的通知公告
     *
     * @param noticeId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("根据id查询通知公告")
    @ApiPageParam
    @PostMapping("/findNotice")
    public Notice findNotice(@RequestParam Long noticeId) {
        return this.noticeService.findNotice(noticeId);
    }


    /**
     * 根据ID查询指定的通知公告(包含外键名称)
     *
     * @param noticeId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("根据ID查询指定的通知公告(包含外键名称)")
    @PostMapping("/findNoticeForView")
    public Notice findNoticeForView(@RequestParam Long noticeId) {
        return this.noticeService.findNoticeWithForeignName(noticeId);
    }

    /**
     * 根据ID查询指定的通知公告(包含通知公告和外键名称)
     *
     * @param noticeId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("根据ID查询指定的通知公告(包含通知公告和外键名称)")
    @PostMapping("/findNoticeForEdit")
    public NoticeEditDto findNoticeForEdit(@RequestParam Long noticeId) {
        NoticeEditDto noticeEditDto = new NoticeEditDto();
        noticeEditDto.setNotice(this.noticeService.findNoticeWithForeignName(noticeId));

        this.prepareNoticeEditDto(noticeEditDto);

        return noticeEditDto;
    }

    /**
     * 根据ID查询指定的通知公告(只提取ID 和 Name)
     *
     * @param noticeId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("根据ID查询指定的通知公告(只提取ID 和 Name)")
    @PostMapping("/findNoticesWithIdNameById")
    public Notice findNoticesWithIdNameById(@RequestParam Long noticeId) {
        return this.noticeService.findNoticesWithIdNameById(noticeId);
    }

    /**
     * 根据名称查询通知公告集合(只提取ID 和 Name)
     *
     * @param noticeName 名称
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:view')")
    @ApiOperation("根据名称查询通知公告集合(只提取ID 和 Name)")
    @PostMapping("/findNoticesWithIdNameByName")
    public List<Notice> findNoticesWithIdNameByName(@RequestParam String noticeName) {
        return this.noticeService.findNoticesWithIdNameByName(noticeName);
    }


    /**
     * 创建新的通知公告
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:add')")
    @ApiOperation("创建新的通知公告")
    @PostMapping("/createNotice")
    public NoticeEditDto createNotice() {
        NoticeEditDto noticeEditDto = new NoticeEditDto();
        noticeEditDto.setNotice(new Notice());

        this.prepareNoticeEditDto(noticeEditDto);
        return noticeEditDto;
    }

    private void prepareNoticeEditDto(NoticeEditDto noticeEditDto) {
    }

    /**
     * 新增保存通知公告
     *
     * @param notice 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:add')")
    @ApiOperation("新增保存通知公告")
    @PostMapping("/saveNotice")
    public Notice saveNotice(@RequestBody Notice notice) {
        return this.noticeService.saveNotice(notice);
    }

    /**
     * 修改保存通知公告
     *
     * @param notice 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:update')")
    @ApiOperation("修改保存通知公告")
    @PostMapping("/updateNotice")
    public Notice updateNotice(@RequestBody Notice notice) {
        return this.noticeService.updateNotice(notice);
    }

    /**
     * 根据ID删除通知公告
     *
     * @param noticeId ID
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Notice:remove')")
    @ApiOperation("根据ID删除通知公告")
    @PostMapping("/deleteNotice")
    public void deleteNotice(@RequestParam Long noticeId) {
        this.noticeService.deleteNotice(noticeId);
    }

    @ApiOperation("发布通知")
    @PostMapping("/pushNotice")
    public JsonResult pushNotice(@RequestParam Long noticeId) {
        return this.noticeService.pushNotice(noticeId);
    }

    @ApiOperation("读取公告")
    @PostMapping("/readNotice")
    public void readNotice(@RequestParam Long noticeId, @RequestParam Long userId) {
        this.noticeService.readNotice(noticeId, userId);
    }
    @ApiOperation("查询未读用户")
    @PostMapping("/findNotReadNoticeUser")
    public JsonResult findNotReadNoticeUser(@RequestParam Long noticeId){
        return this.noticeService.findNotReadNoticeUser(noticeId);
    }
    @ApiOperation("查询已读用户")
    @PostMapping("/findReadNoticeUser")
    public JsonResult findReadNoticeUser(@RequestParam Long noticeId){
        return this.noticeService.findReadNoticeUser(noticeId);
    }
}

